// (c) Copyright 2013-2014 Stephen Gold <sgold@sonic.net>
// Distributed under the terms of the GNU General Public License

/*
 This file is part of the Bats Game.

 The Bats Game is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by the
 Free Software Foundation, either version 3 of the License, or (at your
 option) any later version.

 The Bats Game is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 for more details.

 You should have received a copy of the GNU General Public License
 along with the Bats Game.  If not, see <http://www.gnu.org/licenses/>.
 */
package bats.map;

import bats.scene.SceneNode;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import java.util.logging.Logger;
import jme3utilities.MySpatial;
import jme3utilities.SimpleControl;

/**
 * A simple control used to update the position of a simple water geometry.
 * <p>
 * Each instance is enabled at creation.
 *
 * @author Stephen Gold <sgold@sonic.net>
 */
public class WaterControl
        extends SimpleControl {
    // *************************************************************************
    // constants

    /**
     * message logger for this class
     */
    final private static Logger logger =
            Logger.getLogger(WaterControl.class.getName());
    // *************************************************************************
    // fields
    /**
     * controlling node: set by constructor
     */
    final private Node controllingNode;
    /**
     * orientation from previous update()
     */
    private Quaternion lastOrientation = null;
    /**
     * location from previous update()
     */
    private Vector3f lastLocation = null;
    // *************************************************************************
    // constructors

    /**
     * Instantiate an enabled control for a specified controlling node.
     *
     * @param controllingNode which node will control the position (not null)
     */
    public WaterControl(Node controllingNode) {
        assert controllingNode != null;
        this.controllingNode = controllingNode;

        assert isEnabled();
    }
    // *************************************************************************
    // new methods exposed

    /**
     * Access the controlling node.
     *
     * @return the pre-existing node (not null)
     */
    public Node getControllingNode() {
        assert controllingNode != null;
        return controllingNode;
    }
    // *************************************************************************
    // SimpleControl methods

    /**
     * Callback invoked when the spatial's geometric state is about to be
     * updated, once per frame while attached and enabled.
     *
     * @param updateInterval time interval between updates (in seconds, &ge;0)
     */
    @Override
    protected void controlUpdate(float updateInterval) {
        super.controlUpdate(updateInterval);
        assert spatial instanceof Geometry;
        assert controllingNode != null;

        Vector3f location = MySpatial.getWorldLocation(controllingNode);
        Quaternion orientation = MySpatial.getWorldOrientation(controllingNode);
        if (location.equals(lastLocation)
                && orientation.equals(lastOrientation)) {
            return;
        }
        lastLocation = location;
        lastOrientation = orientation;
        Geometry surface = (Geometry) spatial;
        /*
         * Re-position the surface to match the controlling node.
         */
        MySpatial.setWorldLocation(surface, location);
        MySpatial.setWorldOrientation(surface, orientation);
        /*
         * Update the geometry's material.
         */
        SceneNode.find(spatial).getWater().setMaterial(surface);
    }
}